﻿using HandyControl.Controls;
using Minotaur.App.Core.Contrls;
using Minotaur.App.Domains.Dto;
using SqlSugar;
using System.Collections;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Minotaur.App.Modules.SystemControl.Views
{
    /// <summary>
    /// Interaction logic for SideMenu
    /// </summary>
    public partial class SideMenu : UserControl
    {
        public SideMenu()
        {
            InitializeComponent();
        }
        #region 注册属性
        /// <summary>
        /// 注册数据源
        /// </summary>
        public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(SideMenu), new System.Windows.PropertyMetadata(null, OnItemsSourceChanged));

        /// <summary>
        /// 数据源信息
        /// </summary>
       /// [Bindable(true)]
        public IEnumerable ItemsSource
        {
            get
            {
                return (IEnumerable)GetValue(ItemsSourceProperty);
            }
            set
            {
                SetValue(ItemsSourceProperty, value);
            }
        }
        #endregion

        #region 注册方法
        /// <summary>
        /// 苏剧变更
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((SideMenu)d).OnItemsSourceChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue);
        }

        protected virtual void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
        {
            if (newValue != null)
            {
                this.SideMenuGrid.Children.Clear();
                foreach (var item in newValue)
                {
                    MenuInfoDto menuInfoDto = item as MenuInfoDto;
                    if (menuInfoDto.Childs != null && menuInfoDto.Childs.Any())
                    {
                        var ite = new SideExpander(menuInfoDto);
                        ite.OpendMenuClick += OpendMenuClicked;
                        this.SideMenuGrid.Children.Add(ite);
                    }
                    else
                    {
                        var ite = new SideRadioButton(menuInfoDto);
                        ite.OpendMenuClick += OpendMenuClicked;
                        this.SideMenuGrid.Children.Add(ite);
                    }
                }
            }
        }

        private void OpendMenuClicked(object sender, RoutedEventArgs e)
        {
            MenuInfoDto infoDto = e.OriginalSource as MenuInfoDto;
            RoutedEventArgs args = new RoutedEventArgs(OpendMenuEvent, infoDto);
            RaiseEvent(args);
        }

        //注册打开菜单节点配置事件
        public static readonly RoutedEvent OpendMenuEvent =
              EventManager.RegisterRoutedEvent(ClickNameConst.OpendMenuClick, RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SideMenu));

        /// <summary>
        /// 跳转菜单对应路由页面
        /// </summary>
        public event RoutedEventHandler OpendMenuClick
        {
            add { AddHandler(OpendMenuEvent, value); }
            remove { RemoveHandler(OpendMenuEvent, value); }
        }
        #endregion
    }
}
